﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using IKEA.FileExchange.Domain;
using IKEA.FileExchange.Domain.FilePackage;
using IKEA.FileExchange.Domain.DownloadTickets;
using IKEA.FileExchange.Domain.Files;

public partial class services_DownloadTicketProcessingService : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ticketIdStr = Request.QueryString["t"];
        string fileIdStr = Request.QueryString["f"];
        string packageIdStr = Request.QueryString["p"];

        Guid ticketId = new Guid(ticketIdStr);
        Guid fileId = new Guid(fileIdStr);
        Guid packageId = new Guid(packageIdStr);

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite spSite = new SPSite(Request.Url.AbsoluteUri))
            {
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    SPList ticketsList = IKEAFileStoreHelper.GetDownloadTicketsList(spWeb);
                    SPList packageList = IKEAFileStoreHelper.GetFilePackagesList(spWeb);

                    SPListItem ticket = ticketsList.GetItemByUniqueId(ticketId);
                    SPListItem package = packageList.GetItemByUniqueId(packageId);

                    FilePackageInformation packageInfo = FilePackageInformationProvider.Extract(package);

                    SPDocumentLibrary store = null;

                    if (packageInfo.IsConfidential)
                    {
                        store = IKEAFileStoreHelper.GetConfidentialFileStore(spWeb);
                    }
                    else
                    {
                        store = IKEAFileStoreHelper.GetPublicFileStore(spWeb);
                    }

                    SPListItem file = store.GetItemByUniqueId(fileId);

                    if (file != null)
                    {
                        Response.Clear();

                        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.File.Name);

                        Response.ContentType = "application/octet-stream";

                        using (System.IO.Stream fileStream = file.File.OpenBinaryStream())
                        {
                            using (System.IO.StreamReader sr = new System.IO.StreamReader(fileStream))
                            {
                                string data = sr.ReadToEnd();

                                Response.Write(data);
                            }
                        }

                        SPList downloadHistoryList = IKEAFileStoreHelper.GetDownloadTicketHistoryList(spWeb);

                        spWeb.AllowUnsafeUpdates = true;

                        SPListItem historyItem = downloadHistoryList.Items.Add();

                        historyItem["Title"] = Guid.NewGuid();
                        historyItem[DownloadTicketFieldNames.DownloadTicketOwnerEmail] = ticket[DownloadTicketFieldNames.DownloadTicketOwnerEmail];
                        historyItem[DownloadTicketFieldNames.IKEAFileName] = file.File.Name;
                        historyItem[FileFieldNames.FilePackageReference] = new SPFieldLookupValue(package.ID, package.Title);
                        
                        historyItem.Update();
                    }
                    
                }
            }
        }
        );
        
    }
}
